package com.cp.helper.controller;

import com.cp.helper.bean.Tag;
import com.cp.helper.dao.impl.TagCategoryDao;
import com.cp.helper.dao.impl.TagDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述:
 * @Author : chenpeng
 * @Date : 2015/10/22 14:53
 */
@Controller
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagDao tagDao;
    @Autowired
    private TagCategoryDao tagCategoryDao;

    @RequestMapping("/get_tag_list")
    @ResponseBody
    public Object getTagList(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        int userId = 1;
        Map<String, Object> qryMap = new HashMap<String, Object>();
        qryMap.put("statusFlag",1);
        qryMap.put("userId",userId);

        List<Tag> tagList = tagDao.selectList(qryMap);
        map.put("success", true);
        map.put("tagList", tagList);
        return map;
    }

    @RequestMapping("/delete_tag")
    @ResponseBody
    public Object deleteTag(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        int tagId = Integer.valueOf(request.getParameter("tagId"));
        Tag tag = tagDao.selectByKey(tagId);
        tag.setStatusFlag(2);
        tagDao.updateStatusFlagById(tag);

        map.put("success", true);
        return map;
    }

    @RequestMapping("/add_tag")
    @ResponseBody
    public Object addTag(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        int userId = 1;
        String content = request.getParameter("content");
        if(StringUtils.isEmpty(content)){
            map.put("success",false);
            return map;
        }
        Tag tag = new Tag();
        tag.setContent(content);
        tag.setStatusFlag(1);
        tag.setUserId(userId);
        tag.setCreateTime(new Timestamp(System.currentTimeMillis()));
        tagDao.insert(tag);
        map.put("tag",tag);
        map.put("success", true);
        return map;
    }
}
